package org.practice.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.practice.dao.ShopDaoFactory;
import org.practice.dao.UserDao;
import org.practice.model.User;
import org.practice.util.Constant;
import org.practice.util.CookieUtil;

public class LoginInfoAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		boolean isLogin=false;
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Constant.SESSION_KEY_USER);
		
		if (user != null) {
			isLogin = true;
		} else {
			String uid = CookieUtil.findCookie(request, Constant.COOKIE_KEY_USERID);
			if (uid != null) {
				UserDao userDao = ShopDaoFactory.createUserDao();
				user = userDao.findUserById(Integer.parseInt(uid));
				if (user != null) {
					session.setAttribute(Constant.SESSION_KEY_USER, user);
					isLogin = true;
				}
			}
		}
		request.setAttribute("isLogin", isLogin);
		return mapping.findForward("success");
	}
	
}
